<?php
/**
 * 商品 业务层
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-09-05
 * Time: 17:58
 */

namespace App\Services\Manager;


use App\Models\NoteRecord;
use App\Models\Notice;
use App\Models\Sale;
use App\Models\Shop;
use App\Models\Template;
use App\Models\User;

class NoteService
{
    public function addNoteNumNoteRecord($addNoteNum, $shopId)
    {
        $time = date("Y-m-d  H:i:s");
        $mainShopId = Shop::where('shopId', $shopId)->value('mainShopId');
        if (!$mainShopId) {
            $mainShopId = $shopId;
        }
        $mainShop = Shop::where('shopId', $mainShopId)->first();
        if (!$mainShop) {
            return -1;
        }
        $noteNum = $mainShop['noteTotalNum'] - $mainShop['noteUseNum'];
        if ($noteNum < $addNoteNum) {
            return -2;
        }
        Shop::where('shopId', $mainShopId)->increment('noteUseNum', $addNoteNum);
        if (($noteNum - $addNoteNum) < 100) {
            $addNotice = [];
            $addNotice['title'] = "短信余量通知";
            $addNotice['content'] = "您的剩余短信数量已不足100条，请尽快补充";
            $addNotice['pushTime'] = $time;
            $addNotice['createTime'] = $time;
            $addNotice['shopId'] = $mainShopId;
            Notice::insert($addNotice);
        }
        return 1;
    }

    /**
     * 发送短信
     * @param $data
     * @param $templateId
     */
    public function sendNote($phone, $data, $templateId)
    {
        $array = [];
        switch ($templateId) {
            case 783946:
                $sale = Sale::where('saleNo', $data)->first();
                if (!$sale) {
                    return -1;
                }
                $user = User::where('cardNumber', $sale['cardNumber'])->first();
                if (!$user) {
                    return -2;
                }
                $shopName = Shop::where('shopId', $sale['shopId'])->value('shopName');
                $array[] = $sale['userName'];
                $array[] = "[" . ($sale['realGetPrice'] / 100) . "]";
                $array[] = "[" . ($user['balance'] / 100) . "]";
                $array[] = "[" . $user['usableScore'] . "]";
                $array[] = "【" . $shopName . "】";
                $shopId = $sale['shopId'];
                break;
            case 784406:
                $sale = Sale::where('saleNo', $data)->first();
                if (!$sale) {
                    return -1;
                }
                $user = User::where('cardNumber', $sale['cardNumber'])->first();
                if (!$user) {
                    return -2;
                }
                $shopName = Shop::where('shopId', $sale['shopId'])->value('shopName');
                $array[] = $sale['userName'];
                $array[] = "[" . ($sale['realGetPrice'] / 100) . "]";
                $array[] = "[" . ($sale['debtPrice'] / 100) . "]";
                $array[] = "[" . ($user['balance'] / 100) . "]";
                $array[] = "[" . $user['usableScore'] . "]";
                $array[] = "【" . $shopName . "】";
                $shopId = $sale['shopId'];
                break;
            case 784517:
            case 784504:
            case 784004:
                $array[] = $data['userName'];
                $array[] = "【" . $data['shopName'] . "】";
                $array[] = "420-09870989";
                $noteRecordId = $data['noteRecordId'];
                $shopId = $data['shopId'];
                break;
            case 784529:
                $array[] = $data['userName'];
                $array[] = "420-09870989";
                $array[] = "【" . $data['shopName'] . "】";
                $noteRecordId = $data['noteRecordId'];
                $shopId = $data['shopId'];
                break;
            case 783963:
            case 783975:
                $array[] = "【" . $data['shopName'] . "】";
                $array[] = "420-09870989";
                $noteRecordId = $data['noteRecordId'];
                $shopId = $data['shopId'];
                break;
            default:
                if (isset($data['noteRecordId'])) {
                    $noteRecordId = $data['noteRecordId'];
                    $shopId = $data['shopId'];
                    $errorLiuCode = 1;
                } else {
                    $errorLiuCode = 2;
                }
        }
        $time = date("Y-m-d H:i:s");
        if (!isset($errorLiuCode)) {
            $status = app('sms')->sendDate($phone, $array, $templateId);
            if ($status->statusCode != "000000") {
                $statusCode = $status->statusCode;
                $statusMsg = $status->statusMsg;
                $sendStatus = 0;
                Shop::where('shopId',$shopId)->increment('noteUseNum',-1);
            } else {
                $statusCode = null;
                $statusMsg = null;
                $sendStatus = 1;
            }
            $content = Template::where('templateId', $templateId)->value('content');
            $addNote = [];
            if (($templateId == 783946) || ($templateId == 784406)) {
                $addNote['templateId'] = $templateId;
                $addNote['phone'] = $sale['phone'];
                $addNote['userName'] = $sale['userName'];
                $addNote['shopId'] = $sale['shopId'];
                $addNote['publishName'] = $sale['saleName'];
                $addNote['realGetPrice'] = $sale['realGetPrice'];
                $addNote['debtPrice'] = $sale['debtPrice'];
                $addNote['balance'] = $user['balance'];
                $addNote['usableScore'] = $user['usableScore'];
                $addNote['publishTime'] = $time;
                $addNote['createTime'] = $time;
                $addNote['updateTime'] = $time;
                $addNote['noteStatus'] = 1;
                $addNote['shopName'] = $shopName;
                $addNote['noteType'] = "SALE";
                $noteRecordId = NoteRecord::insertGetId($addNote);
            }
            $noteContent = "";
            foreach ($array as $key => $value) {
                $variate = "{" . ($key + 1) . "}";
                $variateName = 'comtent' . $key;
                $$variateName = explode($variate, $content);
                $noteContent = $noteContent . $$variateName[0] . $value;
                $contentNum = strpos($content, $variate);
                $content = substr($content, $contentNum + 3);
            }
            $update = [];
            $update['noteContent'] = $noteContent;
            $update['updateTime'] = $time;
            $update['noteStatus'] = 1;
            $update['sendStatus'] = $sendStatus;
            $update['statusCode'] = $statusCode;
            $update['statusMsg'] = $statusMsg;
            NoteRecord::where('noteRecordId', $noteRecordId)->update($update);
            return 1;
        } else {
            if ($errorLiuCode == 1) {
                $update = [];
                $update['updateTime'] = $time;
                $update['noteStatus'] = 1;
                $update['sendStatus'] = 0;
                $update['statusCode'] = 505;
                $update['statusMsg'] = "未找到短信模板";
                NoteRecord::where('noteRecordId', $noteRecordId)->update($update);
                return 1;
            } else {
                return 0;
            }
        }
    }
}